package com.zheng.springcloud.controller;

import com.zheng.springcloud.pojo.Dept;
import com.zheng.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lenovo
 */
@RestController
public class DeptController {
    @Autowired
    private DiscoveryClient discovery;
    @Autowired
    private DeptService deptService;
    @RequestMapping("/dept/add")
    public boolean addDept(Dept dept){
        return deptService.addDept(dept);
    }
    @RequestMapping("/dept/get/{id}")
    public Dept get(@PathVariable("id")int id){
        return deptService.selectDeptById(id);
    }
    @RequestMapping("/dept/list")
    public ArrayList<Dept> select(){
        return deptService.selectDept();
    }

    @GetMapping("/dept/discovery")
    public Object discovery(){
        List<String> services = discovery.getServices();
        for (String service : services) {
            System.out.println(service);
        }
        List<ServiceInstance> instances = discovery.getInstances("PROVIDER-DEPT-8001");
        for (ServiceInstance instance : instances) {
            System.out.println("instance=>"+instance.getHost()+
                    instance.getInstanceId()+
                    instance.getUri());
        }
        return this.discovery;
    }
}
